﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TorchView.PakViewer.Zip
{
    public abstract class BaseZipNode : IZipNode, IComparable
    {
        private List<IZipNode> _childNodes = new List<IZipNode>();

        public string Name { get; protected set; }
        public string Path { get; protected set; }
        public bool IsDirectory { get; protected set; }

        public void AddChild(IZipNode child)
        {
            _childNodes.Add(child);
        }

        public IZipNode FindChild(string name)
        {
            foreach (IZipNode child in _childNodes)
            {
                if (child.Name.Equals(name))
                    return child;
            }
            return null;
        }

        public bool ChildExist(string name)
        {
            return (FindChild(name) != null);
        }

        public IEnumerable<IZipNode> Children
        {
            get
            {
                return _childNodes;
            }
        }

        int IComparable.CompareTo(object obj)
        {
            IZipNode z = (IZipNode)obj;
            if (this.IsDirectory && !z.IsDirectory)
                return int.MaxValue;
            else if (!this.IsDirectory && z.IsDirectory)
                return int.MinValue;
            else
                return String.Compare(this.Name, z.Name);
        }
    }
}
